package com.eqxiu.ieditor.interceptor;

import java.util.*;

/**
 * @Author LinSH
 * @Description: 集合相关操作
 * @Date: 下午7:27 2018-03-29
 */
public class Iterators {
    public static <A, B> List<B> map(List<A> values, Function<A, B> func) {
        List<B> list = new ArrayList<>();
        for (A value : values) {
            list.add(func.apply(value));
        }
        return list;
    }

    public static <A, B> Set<B> map(Set<A> values, Function<A, B> func) {
        Set<B> set = new HashSet<>();
        for (A value : values) {
            set.add(func.apply(value));
        }
        return set;
    }

    public static <A, B, C, D> Map<C, D> map(Map<A, B> values, Function<Map.Entry<A, B>, Tuple.Tuple2<C, D>> func) {
        Map<C, D> map = new HashMap<>();
        for (Map.Entry<A, B> entry : values.entrySet()) {
            Tuple.Tuple2<C, D> tp = func.apply(entry);
            map.put(tp._1, tp._2);
        }
        return map;
    }

    public static <A, B, C> List<C> mapToList(Map<A, B> values, Function<Map.Entry<A, B>, C> func) {
        List<C> list = new ArrayList<>();
        for (Map.Entry<A, B> entry : values.entrySet()) {
            list.add(func.apply(entry));
        }
        return list;
    }

    public static <A, B, C> Set<C> mapToSet(Map<A, B> values, Function<Map.Entry<A, B>, C> func) {
        Set<C> set = new HashSet<>();
        for (Map.Entry<A, B> entry : values.entrySet()) {
            set.add(func.apply(entry));
        }
        return set;
    }

    public interface Function<A, B> {
        B apply(A value);
    }
}
